package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.CalendarService;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/calendar")
@Tag(name = "预约日历模块")
public class CalendarController {

    @Autowired
    private CalendarService calendarService;

    @Autowired
    private UserService userService;

    @Operation(summary = "获取月度预约日历")
    @GetMapping("/month")
    public Result<Map<String, Object>> getMonthCalendar(
            @Parameter(description = "年份") @RequestParam(required = false) Integer year,
            @Parameter(description = "月份（1-12）") @RequestParam(required = false) Integer month) {
        try {
            // 如果未提供年份和月份，使用当前年月
            if (year == null || month == null) {
                LocalDate now = LocalDate.now();
                year = now.getYear();
                month = now.getMonthValue();
            }

            Map<String, Object> calendar = calendarService.getMonthCalendar(year, month);
            
            if (calendar.containsKey("error")) {
                return Result.error(calendar.get("error").toString());
            }
            
            return Result.success(calendar);
        } catch (Exception e) {
            log.error("获取月度预约日历失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取用户预约日程")
    @GetMapping("/my-schedule")
    public Result<List<Map<String, Object>>> getMySchedule(
            @Parameter(description = "开始日期（YYYY-MM-DD）") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期（YYYY-MM-DD）") @RequestParam(required = false) String endDate) {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();
            var user = userService.getUserByUsername(username);
            if (user == null) {
                return Result.error("用户不存在");
            }

            LocalDate start = startDate != null ? LocalDate.parse(startDate) : null;
            LocalDate end = endDate != null ? LocalDate.parse(endDate) : null;

            List<Map<String, Object>> schedule = calendarService.getUserSchedule(user.getId(), start, end);
            return Result.success(schedule);
        } catch (Exception e) {
            log.error("获取用户预约日程失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}

